package acom.test.swing.bb;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class SystemUtil {

	private static List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();
	private static List<Prize> prizes = new ArrayList<Prize>();

	// 当前 正在抽的 奖
	private static Prize curPrize;

	// 在displayPanel 中 JLabel的总数量
	private static int totalLabelNum = 0;

	// 在displayPanel 中需要显示抽奖名字的 JLabel的总数量
	private static int viewLabelNum = 0;

	public static void showInCenter(JFrame frame) {
		// 显示位置
		int HEIGHT = frame.getHeight();
		int WIDTH = frame.getWidth();
		Toolkit kit = Toolkit.getDefaultToolkit();// 设置顶层容器框架为居中
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		frame.setLocation(x, y);
	}

	public static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = PeoplePanel.class.getResource("image/" + path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	public static boolean isInteger(String sum) {
		String num_regex = "^[0-9]+$";
		return sum.matches(num_regex);
	}

	// 验证文件扩展名 如："txt"
	public static boolean checkFileExtendName(String filename, String patten) {
		String fname = filename;

		int index = fname.lastIndexOf(".");

		String temp = fname.substring(index + 1, fname.length());

		return patten.equals(temp);
	}

	public static String getNowTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static List<AnswerPeople> getPeoplelist() {
		return peoples;
	}

	public static void setPeoplelist(List<AnswerPeople> peoplelist) {
		peoples = peoplelist;
	}

	public static List<Prize> getPrizelist() {
		return prizes;
	}

	public static void setPrizelist(List<Prize> prizelist) {
		prizes = prizelist;
	}

	public static Prize getCurPrize() {
		return curPrize;
	}

	public static void setCurPrize(Prize prize) {
		SystemUtil.curPrize = prize;
	}

	public static Prize findPrizeByName(String prizeName) {

		for (Prize prize : prizes) {
			if (prizeName.equals(prize.getPname())) {
				return prize;
			}
		}

		return null;
	}

	public static int getViewLabelNum() {
		return viewLabelNum;
	}

	public static void setViewLabelNum(int viewLabelNum) {
		SystemUtil.viewLabelNum = viewLabelNum;
	}

	public static int getTotalLabelNum() {
		return totalLabelNum;
	}

	public static void setTotalLabelNum(int totalLabelNum) {
		SystemUtil.totalLabelNum = totalLabelNum;
	}
}
